const app = getApp();

app.func.skulist(
    {
        /**
         * 页面的初始数据
         */
        data: {
            productlist: [],
            categorylist: '',
            pageEnd: false,
            currentTab: 0,
            type: null,   //1推荐   2价格   3折扣  4筛选
            min: null,
            max: null,
            showbox: false,  //隐藏筛选框
            chooseindex: 4,  //筛选框默认选择全部搜索
            sort: '',
            id: 0,
            myinfo:''
        },

        collectChange(e){
           let data = e.detail;
           this.setData({
               [`productlist[${this.data.currentTab}][${data.index}].add_cart`]:data.value
           });
        },

        categories: function () {
            app.func.getPromise('/categories')
                .then(([code, res]) => {
                    if (code == 200) {
                        // res.data.splice(1, 2);
                        this.initData(res);
                    }
                });
        },
        onLoad: function (options) {
            app.func.onPageLoad(this, options);
            console.log(options)

        },

        onShow:function (){
            this.categories();
            this.myinfos();
        },

        initData: function (res) {
            let value = wx.getStorageSync('category_id')
            console.log(value)
            let result = this.packageJson({
                id: value || 0,
                type: 0,
            });
            this.setData({
                pageEnd: false,
                currentTab: value ? res.data.map(item=>item.id).indexOf(value) : 0,
                type: null,
                min: null,
                max: null,
                showbox: false,
                chooseindex: 4,
                navScrollLeft: 0,
                sort: '',
                categorylist: res.data,
            });
            this.requestnav1(result);
            wx.removeStorageSync('category_id');
        },


        preRequestCheckid: function (e) {
            this.getBrandlist(e.target.dataset.id);
            let result = this.packageJson({id: e.target.dataset.id, type: 0});
            this.requestnav1(result);
        },


        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom: function () {
            if (this.data.pageEnd) {
                this.setData({pageEnd: true});
            } else {
                wx.showLoading({title: '加载更多', mask: true});
                let data = this.data;
                let result = this.packageJson({
                    id: data.id,
                    offset: data.productlist[data.currentTab].length,
                    min: data.min,
                    max: data.max,
                    min_rate: data.min_rate,
                    max_rate: data.max_rate,
                    type: data.type,
                    key: data.key,
                    sort: data.sort,
                    brand: data.brand,
                });
                this.requestnav1(result);
            }
        },

        //处理需要提交的数据
        packageJson: function (obj) {
            let id = obj.id === 0 || obj.id ? obj.id : this.data.id;
            let offset = obj.offset || 0;
            let type = obj.type || 0;
            let key = obj.key || '';
            let min = obj.min || 0;
            let max = obj.max || 10000;
            let min_rate = obj.min_rate || '';
            let max_rate = obj.max_rate || '';
            let sort = obj.sort || '';
            let sup_id = obj.sup_id || '';
            let brand = obj.brand || '';
            let result = {
                id: id,
                offset: offset,
                type: type,
                key: key,
                min: min,
                max: max,
                min_rate: min_rate,
                max_rate: max_rate,
                sort: sort,
                length: 20,
                sup_id: sup_id,
                brand: brand
            };
            this.setData({
                id: id, type: type, min: min, max: max, sup_id: sup_id,
                key: key, sort: sort, min_rate: min_rate, max_rate: max_rate,
                brand: brand,
            });
            return result;
        },
        requestnav1: function (obj) {
            app.func.postPromise('/categorySku', obj).then(([code, res]) => {
                let prolist = this.data.productlist;
                let index = this.data.currentTab;
                if (obj.offset === 0) {
                    if (res.data.length === 0) {
                        app.func.toastPromise('该分类下暂无商品');
                    } else {
                        prolist[index] = res.data;
                        this.setData({
                            productlist: prolist,
                            pageEnd: false,
                        });
                        wx.setNavigationBarTitle({title: res.name});
                    }
                } else if (res.data.length === 0) {
                    this.setData({pageEnd: true}, () => wx.hideLoading());
                } else {
                    prolist[index] = prolist[index].concat(res.data);
                    this.setData({productlist: prolist}, () => wx.hideLoading());
                }
            });
        },

        //点击 推荐
        // choosetype: function (e) {
        //     this.scrollTop();
        //     let result = this.packageJson({
        //         brand: this.data.brand,
        //         sup_id: this.data.sup_id,
        //         min: this.data.min,
        //         max: this.data.max,
        //         min_rate: this.data.min_rate,
        //         max_rate: this.data.max_rate,
        //         type: 1
        //     });
        //     this.requestnav1(result);
        // },


        //列表排序
        proListOrder: function (e) {
            this.scrollTop();
            let ordetType = e.currentTarget.dataset.type;
            let sort = this.data.sort;
            if (ordetType == 0) {
                sort = sort && sort == 3 ? 4 : 3;
            } else {
                sort = sort && sort == 1 ? 2 : 1;
            }
            let result = this.packageJson({
                brand: this.data.brand,
                sup_id: this.data.sup_id,
                min: this.data.min,
                max: this.data.max,
                min_rate: this.data.min_rate,
                max_rate: this.data.max_rate,
                sort: sort,
                offset: 0,
            });
            this.setData({
                    sort: sort
                },
                this.requestnav1(result)
            );
        },


        //点击筛选
        chooseall: function () {
            this.companyInfoInit();
            this.setData({showbox: true});
        },
        backbtn: function () {
            this.setData({showbox: false});
        },
        formsubmit(e) {
            this.scrollTop();
            let data = e.detail.value;
            let result = this.packageJson({
                type: this.data.chooseindex,
                key: data.key,
                min: data.minprice,
                max: data.maxprice,
                min_rate: data.minrate,
                max_rate: data.maxrate,
                sup_id: data.company == null ? '' : this.data.company[data.company].id,
                brand: this.data.brand
            });
            this.setData({
                showbox: false
            }, this.requestnav1(result));
        },
        myinfos(){
            app.func.getPromise('/my/info')
                    .then(([code, res]) => {
                    this.setData({
                            myinfo: res.message
                    });
            })
        },
        

    }
);





